package com.example.config;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class ConfirmCallBackImpl implements RabbitTemplate.ConfirmCallback {

    /**
     * @param correlationData 相关数据,一般存储一个id,用来辨识唯一性
     * @param ack             确认,如果成功返回 true,如果失败返回 false
     * @param cause           原因,失败原因
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("correlationData = " + correlationData);
        System.out.println("ack = " + ack);
        System.out.println("cause = " + cause);

        if (ack) {
            System.out.println("投递到交换机成功");
        } else {
            System.out.println("投递失败,原因是:\t" + cause);
            System.out.println("将失败的消息 " + correlationData.getId() + " 保存到数据库");
        }
    }
}
